class Segment :
  def __init__(self, _string, _offset, _lenght, _list_attr) :
    self.str = unicode(_string,'utf-8')
    self.array_attribute = {}
    self.array_attribute['offset'] = _offset
    self.array_attribute['len'] = _lenght
    for attr,val in _list_attr.iteritems() :
      self.array_attribute[attr] = val

  def set_offset(self,o) :
    self.array_attribute['offset'] = o

  def set_len(self,l) :
    self.array_attribute['len'] = l

  def get_offset(self) :
    return self.array_attribute['offset']

  def get_str(self) :
    return self.str

  def get_print_str(self) :
    return self.str.encode('utf-8','replace')

  def get_len(self) :
    return self.array_attribute['len']

  def __cmp__(self,other) :
    diff_offset = self.get_offset() - other.get_offset()
    if(diff_offset == 0) :
      return self.get_len() - other.get_len()
    return diff_offset

  def __str__(self) :
    s = self.array_attribute['offset']
    e = s + self.array_attribute['len']
#    string = self.get_print_str()
    string = self.get_str()
    portion = string[s:e].encode('utf-8','replace')
    return portion

  def str_verbose(self) :
    s = self.array_attribute['offset']
    l = self.array_attribute['len']
    return "[%s:%s:%i:%o]"%(self.str,self.__str__(),s,l)


class Mot(Segment) :
  def str_verbose(self) :
    s = self.array_attribute['offset']
    l = self.array_attribute['len']
    return "[%s:%i:%o]"%(self.__str__(),s,l)

class Chunk(Segment) :
  def __init__(self,_list_mot) :
    self.array_attribute = {}
    self.list_mot = _list_mot
    o = _list_mot[0].get_offset()
    self.set_offset(o)
    self.str = _list_mot[0].get_str()
    o1 = _list_mot[-1].get_offset()
    l1 = _list_mot[-1].get_len()
    self.set_len(o1 - o + l1)

